package com.morehair.controller.wx;


import com.morehair.bean.bo.admin.CouponReq;
import com.morehair.bean.bo.wx.*;
import com.morehair.bean.pojo.Coupon;

import com.morehair.bean.vo.wx.WxBaseData;
import com.morehair.bean.vo.admin.BaseRespVo;
import com.morehair.bean.vo.wx.WxCheckoutVo;
import com.morehair.service.wx.WxCartService;
import com.morehair.service.wx.WxCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/7 23:24
 * @Description:
 **/

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    @RequestMapping("list")
    public BaseRespVo couponList(WxCouponReq wxCouponReq) {

        WxBaseData<Coupon> wxCouponData = wxCouponService.wxQuery(wxCouponReq);
        return BaseRespVo.ok(wxCouponData);
    }

    @RequestMapping("mylist")
    public BaseRespVo myList(WxCouponReq wxCouponReq) {
        WxBaseData<Coupon> wxCouponData = wxCouponService.wxQueryMyCoupon(wxCouponReq);
        return BaseRespVo.ok(wxCouponData);
    }

    @PostMapping("receive")
    public BaseRespVo couponReceive(@RequestBody CouponReceiveBO couponReceiveBO) {
//        Integer couponId = couponReceiveBO.getCouponId();
        Integer result = wxCouponService.couponReceice(couponReceiveBO);

        if (result == 1) {
            return BaseRespVo.fail("优惠券已经领完");
        } else if (result == 2) {
            return BaseRespVo.fail("已领过该优惠券");
        } else if (result == 3) {
            return BaseRespVo.fail("当前时间不能领");
        }
        return BaseRespVo.ok("领取成功");
    }


    @RequestMapping("exchange")
    public BaseRespVo couponExchange(@RequestBody WxCouponExchangeBO wxCouponExchangeBO) {
        Integer resultCode = wxCouponService.couponExchange(wxCouponExchangeBO);
        if (resultCode == 4) {
            return BaseRespVo.fail("兑换码无效");
        } else if (resultCode == 1) {
            return BaseRespVo.fail("优惠券已经领完");
        } else if (resultCode == 2) {
            return BaseRespVo.fail("已领过该优惠券");
        } else if (resultCode == 3) {
            return BaseRespVo.fail("当前时间不能领");
        }
        return BaseRespVo.ok("领取成功");
    }

    @Autowired
    WxCartService wxCartService;

    @RequestMapping("selectlist")
    public BaseRespVo selectList(WxCouponSelectBO wxCouponSelectBO) {
        WxCheckoutVo wxCheckoutVo = wxCartService.checkoutOrder(wxCouponSelectBO.getCartId(), null, null, wxCouponSelectBO.getGrouponRulesId());

        List<Coupon> availableCoupons = wxCouponService.couponSelect(wxCouponSelectBO, wxCheckoutVo);
        return BaseRespVo.ok(availableCoupons);
    }
}
